﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CPPEI.Coolzon.ServiceProxy.DataServiceRef;
using Visifire.Gauges;
using System.Windows.Media.Animation;
using CPPEI.Coolzon.Common;
using System.Windows;
using System.Windows.Media;

namespace CPPEI.Coolzon.UI.DataPartLib
{
    public partial class DataGaugeBase : DataPart
    {
        public DataGaugeBase()
        {
            MajorTicksBackground = new BrushEx(new SolidColorBrush(Colors.Black));
            MinorTicksBackground = new BrushEx(new SolidColorBrush(Colors.Black));
            CircularScaleLabelsFontSize = 12;

            CircularScaleLabelsFontColor = new BrushEx(new SolidColorBrush(Colors.Black));
            CircularScaleLabelsFontFamily = new FontFamily("微软雅黑");

            NeedleCapBackground = new BrushEx(new SolidColorBrush(Colors.Gray));
            NeedleBackground = new BrushEx(new SolidColorBrush(Colors.Orange));

            SetDefaultDataResult();
        }
        protected virtual void SetDefaultDataResult()
        {
            Random random = new Random();
            ServiceProxy.DataServiceRef.DataSeries[] series = new ServiceProxy.DataServiceRef.DataSeries[1];
            ServiceProxy.DataServiceRef.DataItem[] tmpItems = new ServiceProxy.DataServiceRef.DataItem[1];
            for (int i = 1; i <= 1; i++)
            {
                tmpItems[i - 1] = new ServiceProxy.DataServiceRef.DataItem();
                tmpItems[i - 1].Id = "data" + i;
                tmpItems[i - 1].Name = "data" + i;
                tmpItems[i - 1].Value = (random.NextDouble() * 120).ToString("F2");
            }
            series[0] = new ServiceProxy.DataServiceRef.DataSeries();
            series[0].DataItems = tmpItems;
            series[0].Id = "series1";
            series[0].Name = "series1";
            DataResult = new ServiceProxy.DataServiceRef.DataResult();
            DataResult.DataSeries = series;
        }

        protected override void OnApplyXProperty()
        {
            base.OnApplyXProperty();

            Gauge gauge = GetGauge();
            if(gauge != null)
            {
                gauge.AnimationEnabled = false;
                SetGauge(gauge);
                SetRange(gauge);
            }
        }

        protected override void OnDataBinding(DataResult dataResult)
        {
            Gauge gauge = GetGauge();
            gauge.Indicators.Clear();

            foreach(var dataSeries in dataResult.DataSeries)
            {
                foreach(var dataItem in dataSeries.DataItems)
                {
                    Indicator indicator;
                    switch (GetIndicatorType(dataSeries.Name))
                    {
                        case IndicatorTypes.Needle:
                            indicator = new NeedleIndicator();
                            SetNeedleIndicator((NeedleIndicator)indicator);
                            break;
                        case IndicatorTypes.Marker:
                            indicator = new MarkerIndicator();
                            SetMarkerIndicator((MarkerIndicator)indicator);
                            break;
                        case IndicatorTypes.Bar:
                            indicator = new BarIndicator();
                            SetBarIndicator((BarIndicator)indicator);
                            break;
                        default:
                            indicator = new NeedleIndicator();
                            SetNeedleIndicator((NeedleIndicator)indicator);
                            break;
                    }

                    gauge.Indicators.Add(indicator);

                    indicator.Value = 0;
                    indicator.BeginAnimation(NeedleIndicator.ValueProperty, new DoubleAnimation(0, Convertor.ToDouble(dataItem.Value), new Duration(TimeSpan.FromSeconds(XDataAnimationDuration))));
                }
            }
        }

        protected virtual Gauge GetGauge()
        {
            return null;
        }
        internal virtual IndicatorTypes GetIndicatorType(string dataSeriesName)
        {
            return IndicatorTypes.Needle;
        }

        internal enum IndicatorTypes
        {
            Needle,
            Bar,
            Marker,
        }
    }
}
